package com.cnhis.onehis;

import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class LuceneTestSearch
{

   /**
    * 多条件查询
    * @param indexDir
    * @param q
    * @throws Exception
    */
   public static void searchBooleanQuery( String indexDir, String q ) throws Exception
   {
      Directory directory = FSDirectory.open( Paths.get( indexDir ) );
      IndexReader indexReader = DirectoryReader.open( directory );
      IndexSearcher indexSearcher = new IndexSearcher( indexReader );
      Analyzer analyzer = new IKAnalyzer(false);
      // 多条件必备神器
      BooleanQuery.Builder builder = new BooleanQuery.Builder();
      // 实际使用中一般是多目标搜索（根据 姓名、性别、年龄、学校等等），
//      QueryParser parser = new MultiFieldQueryParser( new String[]{"contents","fullPath"}, analyzer );
      // 条件一
      QueryParser parser = new MultiFieldQueryParser( new String[]{"bbx05","shouzimu"}, analyzer);
      Query query = parser.parse(q+"*");
      // contents必须含有条件一
      builder.add( query, Occur.MUST );
      // 条件二
      QueryParser parser1 = new QueryParser( "bda01", analyzer );
      Query query1 = parser1.parse("E");
      // fileName必须不能是 b 和 c
      builder.add( query1, Occur.MUST );

      TopDocs topDocs = indexSearcher.search( builder.build(), 10 );

      for ( ScoreDoc scoreDoc : topDocs.scoreDocs )
      {
         Document document = indexSearcher.doc( scoreDoc.doc );
         System.out.println( document.get( "bbx05" ) );
      }
      //-------------------------------------------
      /*BooleanQuery.Builder builder1 = new BooleanQuery.Builder();
      WildcardQuery wildcardQuery = new WildcardQuery(new Term("shouzimu",q+"*"));
      builder1.add(wildcardQuery, Occur.MUST);
      WildcardQuery wildcardQuery1 = new WildcardQuery(new Term("bda01","E"));
      builder1.add(wildcardQuery1, Occur.MUST);
      TopDocs topDocs1 = indexSearcher.search( builder1.build(), 10 );*/
      BooleanQuery.Builder builder1 = new BooleanQuery.Builder();
      QueryParser parser3 = new MultiFieldQueryParser( new String[]{"bbx05"}, analyzer);
      Query query3 = parser3.parse(q);
      // contents必须含有条件一
      builder1.add( query3, Occur.MUST );
      // 条件二
      QueryParser parser2 = new QueryParser( "bda01", analyzer );
      Query query2 = parser2.parse("E");
      // fileName必须不能是 b 和 c
      builder1.add( query2, Occur.MUST );

      TopDocs topDocs1 = indexSearcher.search( builder1.build(), 10 );

      for ( ScoreDoc scoreDoc : topDocs1.scoreDocs )
      {
         Document document = indexSearcher.doc( scoreDoc.doc );
         System.out.println( document.get( "bbx05" ) );
      }
      indexReader.close();
   }

   public static void main( String[] args )
   {
      String indexDir = "D:\\luceneTest";
      String q = "jzww";
      try
      {
         // 搜索contents含有1 但是除开文件b 和  c
         System.out.println( "多条件查询：" );
         searchBooleanQuery( indexDir, q );
      }
      catch ( Exception e )
      {
         e.printStackTrace();
      }
   }
}